from PyQt5.QtCore import QThread,pyqtSignal,QTimer
import cv2
from utils import handsutils
from utils.handservo import handServo

class CamareWorker(QThread):
    sinOut = pyqtSignal(memoryview,int,int,int)
    isOpend=False       #是否第一次启动摄像头
    capture=None        #摄像头

    def __init__(self, parent=None):
        super(CamareWorker, self).__init__(parent)
        self.dirver=handServo()

    def run(self):
        while True:
            #摄像头没有打开则打开摄像头
            if not self.isOpend:
                self.capture = cv2.VideoCapture(0)
                self.isOpend=True
                continue
            ret, frame = self.capture.read()
            if ret:
                frame = handsutils.mediapipe_gestures(cv2.flip(frame, 1),self.dirver)
                height, width, channel = frame.shape
                bytesPerLine = 3 * width
                self.sinOut.emit(frame.data,width, height, bytesPerLine)